package com.init.engineering.controller;

import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pages.Result;
import com.init.engineering.pojo.Subitem;
import com.init.engineering.service.SubitemService;
import com.init.engineering.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/subitem")
public class SubitemController {

    @Autowired
    private SubitemService subitemService;

    @PostMapping("/all")
    public PageResult findSubitemByPages(@RequestBody QueryPageBean queryPageBean){
        PageResult stagesByPage = subitemService.findSubitemsByPage(queryPageBean);
        return stagesByPage;
    }


    @PostMapping("/add")
    public Result addSubitem(@RequestBody Subitem subitem){

        //随机ID
        subitem.setSuid(UUIDUtil.GetID());
        //获取系统更新时间
        subitem.setUpdatetime(new Date());
        int row = subitemService.saveSubitem(subitem);
        Result result = new Result();
        if(row > 0){
            result.setMessage("添加成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("添加失败，请重试");
        }
        return result;
    }


    @GetMapping("/delete/{suid}")
    public Result deleteSubitem(@PathVariable("suid") String suid){
        int row = subitemService.deleteSubitem(suid);
        Result result = new Result();
        if(row > 0){
            result.setMessage("删除成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("删除失败，请重试");
        }
        return result;
    }


    @GetMapping("/findSubitemBySuid")
    public Result findSubitemBySuid(String suid){
        Subitem subitem = subitemService.findSubitemBySuid(suid);
        Result result = new Result();
        if (subitem != null){
            result.setFlag(true);
            result.setMessage("回显成功");
            result.setData(subitem);
        }else{
            result.setFlag(false);
            result.setMessage("数据回显失败");
        }
        return result;
    }


    @PostMapping("/update")
    public Result updateSubitem(@RequestBody Subitem subitem){
        subitem.setUpdatetime(new Date());
        int row = subitemService.updateSubitem(subitem);
        Result result = new Result();
        if(row > 0){
            result.setMessage("更新成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("更新失败，请重试");
        }
        return result;
    }

    @RequestMapping("/findSubitems")
    public Result findSubitems(){
        List<Subitem> subitems = subitemService.findSubitems();
        Result result = new Result();
        if(subitems != null){
            result.setData(subitems);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }
}
